
public class MySingleLinkedList {

    static class ListNode{
        //内部类,在链表中相当于节点,与C语言中的链表作比较相当于节点的结构体
        public int val;//节点中存储的数据
        public ListNode next;//存储下一个节点的地址

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;//代表链表的头节点

    //创建链表
    public void createList(){
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        this.head = node1;
    }

    //遍历链表
    public void display(){
        ListNode cur = head;//用于存储第一个节点的值,方便于遍历链表
        while(cur != null){
            System.out.print(cur.val +  " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //求链表长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count ++;
            cur = cur.next;
        }
        return count;
    }

    //头插节点,先修改后面的,再修改前面的
    public void addFirst(int val){
        ListNode node = new ListNode(val);
        node.next = head;
        head = node;
    }

    //尾插,还要判断是否为空
    public void addLast(int val) {
        ListNode node = new ListNode(val);
        if (head == null) {
            head = node;
            return;
        }
            ListNode cur = head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = node;
    }

    //在链表任意位置插入节点
    public void addIndex(int index,int val){
        //先判断index的合法性
        try{
            checkIndex(index);
        }catch(IndexNotLegalException e){
            e.printStackTrace();
        }
        //index == 0 || index == size();
        if(index == 0){
            addFirst(val);
            return;
        }
        if(index == size()){
            addLast(val);
            return;
        }
        //找到index的前一个位置
        ListNode cur = findIndexSubOne(index);
        //连接节点
        ListNode node = new ListNode(val);
        node.next = cur.next;
        cur.next = node;
    }

    //找到要插入的节点的前一个节点
    private ListNode findIndexSubOne(int index){
        ListNode cur = head;
        int count = 0;
        while(count != index - 1){
             cur = cur.next;
             count ++;
         }
        return cur;
    }

    //该方法用于判断index是否合法
    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new IndexNotLegalException("index不合法");
        }
    }

    //判断链表是否包含某个数据
    public boolean contain(int val){
        ListNode cur = head;
        while(cur != null){
            if (cur.val == val){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现val的节点
    public void remove(int val){
        //如果头节点是空的
        if(head == null){
            return;
        }
        //如果要删除的节点是头节点
        if(head.val == val){
            head = head.next;
            return;
        }
        //找到当前需要删除值的节点的前一个
        ListNode cur = head;
        while(cur.next != null){
            if(cur.next.val == val){
                ListNode del = cur.next;
                cur.next = del.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除链表中所有的key节点(可以遍历链表,然后每一个跟key相同的值,创建新的链表)
    //在原有链表的基础上实现
    public void removeAllKey(int key){
        //先判断链表是否为空
        if (head == null){
            return;
        }
        //定义当前节点和前驱节点
        ListNode cur = head.next;
        ListNode prev = head;
        //开始判断并且进行删除
        while(cur != null){
            if ((cur.val == key)){
                prev.next = cur.next;
            }else{
                prev = cur;
            }
            cur = cur.next;
        }
        //处理头节点
        if(head.val == key){
            head = head.next;
        }
    }

    //清空链表
    public void clear(){
    //最简单的做法,直接将head = null
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }
        head = null;
    }
}
